import 'dart:convert';

import 'package:flutter/material.dart';
// import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:umeng_push_sdk/umeng_push_sdk.dart';

import 'package:yt_devops/configs.dart';
import 'package:yt_devops/pages/alarm/alarm_home/alarm_home_controller.dart';
import 'package:yt_devops/pages/site/site_controller.dart';
import 'package:yt_devops/routers/router_path.dart';
import 'package:yt_devops/utils/constants/store_keys.dart';
import 'package:yt_devops/utils/enums.dart';
import 'package:yt_devops/utils/navigate_utils.dart';

class UmengPusher {
  static int badgeNumber = 0;

  static const _TAG = "PushSample";

  static openUmengLog() {
    UmengPushSdk.setLogEnable(true);
  }

  static closeUmengLog() {
    UmengPushSdk.setLogEnable(false);
  }

  static register() async {
    await Configs().loadConfigs();
    String appkey = '665599058d52370b9d98787a';

    await UmengPushSdk.register(appkey, "AppStore");
  }

  static Future<String?> getDeviceToken() async {
    String? deviceToken = await UmengPushSdk.getRegisteredId();
    GetStorage storage = GetStorage();
    storage.write(StoreKeys.pushDeviceToken, deviceToken);
    print("$_TAG deviceToken result: $deviceToken");
    return deviceToken;
  }

  static Future<bool?> addBadge() async {
    bool? result = await UmengPushSdk.setBadge(++badgeNumber);
    print("$_TAG reduceBadge $badgeNumber result: $result");
    return result;
  }

  static Future<bool?> reduceBadge() async {
    if (--badgeNumber < 0) {
      badgeNumber = 0;
    }
    bool? result = await UmengPushSdk.setBadge(badgeNumber);

    print("$_TAG reduceBadge $badgeNumber result: $result");
    return result;
  }

  static Future<bool?> clearBadge() async {
    badgeNumber = 0;
    bool? result = await UmengPushSdk.setBadge(badgeNumber);

    print("$_TAG clearBadge result: $result");

    return result;
  }

  static openPush() async => UmengPushSdk.setPushEnable(true);

  static closePush() async => UmengPushSdk.setPushEnable(false);

  static Future<bool?> addAlias(String alias, String type) async =>
      await UmengPushSdk.addAlias(alias, type);

  static Future<bool?> removeAlias(String alias, String type) async =>
      await UmengPushSdk.removeAlias(alias, type);
  static Future<bool?> setAlias(String alias, String type) async =>
      await UmengPushSdk.setAlias(alias, type);
  static Future<bool?> addTags(List<String> tags) async =>
      await UmengPushSdk.addTags(tags);

  static Future<bool?> removeTags() async =>
      await UmengPushSdk.removeTags(["myTag1", "myTag2", "myTag3"]);

  static Future<List<dynamic>?> getAllTags() async =>
      await UmengPushSdk.getTags();

  // 设置推送
  static Future<void> setLogEnable() async =>
      await UmengPushSdk.setLogEnable(true);

  static Future<void> setPushEnable() async =>
      await UmengPushSdk.setLogEnable(true);

  static Future<void> setPushDisable() async =>
      await UmengPushSdk.setLogEnable(false);

  static setTokenCallback() {
    UmengPushSdk.setTokenCallback((deviceToken) {
      GetStorage storage = GetStorage();
      storage.write(StoreKeys.pushDeviceToken, deviceToken);
      print("$_TAG pushDeviceToken:" + deviceToken);
    });
  }

  static setMessageCallback() {
    UmengPushSdk.setMessageCallback((msg) {
      print("$_TAG receive custom:" + msg);
    });
  }

  static setNotificationCallback() {
    UmengPushSdk.setNotificationCallback((receive) {
      debugPrint("$_TAG receive:$receive");
    }, (open) {
      var extra = jsonDecode(open);
      debugPrint("extra====${extra['id']}");
      // 跳去任务详情
      if (extra['url'].contains(RouterPath.taskDetail)) {
        NavigateUtils.toTaskDetail(
            extra['id'], $enumDecodeNullable($TaskTypeEnumMap, extra['type'])!);
      } else if (extra['url'].contains(RouterPath.alarmDetail)) {
        debugPrint("extra['id']====${extra['id']}");
        GetStorage storage = GetStorage();
        bool isInit = storage.read(StoreKeys.appIsInit) ?? false;
        if (isInit) {
          NavigateUtils.toAlarmDetail(extra['id']);
        } else {
          Future.delayed(Duration(seconds: 3)).then((value) {
            NavigateUtils.toAlarmDetail(extra['id']);
          });
        }
      }
      debugPrint("$_TAG open:$open");
    });
  }
}
